#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/io.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>

struct kobject *root;
struct kobject *child;
struct kobj_attribute etx_attr;

int red = 147;

static ssize_t red_show(struct kobject *kobj, struct kobj_attribute *attr,
		char *buf) {
	return sprintf(buf, "%d", red);
}

static ssize_t red_store(struct kobject *kobj, struct kobj_attribute *attr,
		const char *buf, size_t count) {
	pr_info("red_store\n");

	if (kstrtoint(buf, 10, &red) || red < 1 || red > 255) {
		return -EINVAL;
	}

	return count;
}

struct kobj_attribute etx_attr =
__ATTR(red, S_IWUSR | S_IRUGO , red_show, red_store);

inline int myinit(void) {
	root = kobject_create_and_add("mysys001", NULL);
	child = kobject_create_and_add("child01", root);

	if (sysfs_create_file(child, &etx_attr.attr)) {
		printk(KERN_INFO"Cannot create sysfs file......\n");
		return -1;
	}

	return 0;
}

static __init int my_init(void) {
	return myinit();
}

static void __exit my_exit(void) {
	sysfs_remove_file(child,&etx_attr.attr);
	kobject_put(child);
	kobject_put(root);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("a sample driver");
